package com.shengqiang.cloud.ups.controller;

import com.shengqiang.cloud.ups.common.constant.UpmsResultConstant;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsUser;
import com.shengqiang.cloud.ups.service.UpsUserWebService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2017/11/15.
 */
@Controller
@RequestMapping("/admin")
public class UpsController {

    @Autowired
    private UpsUserWebService service;


    /**
     * 注册
     * @param upmsUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register",method = {RequestMethod.GET,RequestMethod.POST})
    public JsonResult register(UpmsUser upmsUser){

        // 创建盐, 散列加密
        String salt = String.valueOf(System.currentTimeMillis());
        SimpleHash password = new SimpleHash("MD5", upmsUser.getPassword(), salt);
        upmsUser.setSalt(salt);
        upmsUser.setPassword(password.toString());
        boolean flag=service.addUser(upmsUser);
        if(flag){
            return new JsonResult(UpmsResultConstant.SUCCESS.code,"注册成功");
        }else {
            return new JsonResult(UpmsResultConstant.FAILED.code,"注册失败");
        }
    }


    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="login",method = {RequestMethod.POST,RequestMethod.GET})
    public JsonResult login(String username,String password,boolean rememberMe){
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password,rememberMe);
        try{
            subject.login(token);
        }catch (AuthenticationException ex){
            String simpleName = ex.getClass().getSimpleName();
            if ("UnknownAccountException".equals(simpleName)) {
                return new JsonResult(UpmsResultConstant.INVALID_USERNAME.code,"用户名不存在");
            } else if("IncorrectCredentialsException".equals(simpleName)){
                return new JsonResult(UpmsResultConstant.INVALID_PASSWORD.code,"密码不正确");
            }
        }
        boolean authenticated = subject.isAuthenticated();
        if (authenticated) {
            return new JsonResult(UpmsResultConstant.SUCCESS.code,"登录成功,正在跳转.....");
        }
        return new JsonResult(UpmsResultConstant.FAILED.code,"很抱歉,你没有改系统权限！！！");
    }

    @RequestMapping(value = "index")
    public String index(ModelMap map){
        map.addAttribute("host","http://www.baidu.com");
        return "index";
    }

    @RequestMapping(value = "toLogin")
    public String redirectLogin(){
        return "login";
    }

    @RequestMapping(value = "main")
    public String toMain(){
        return "main";
    }
}
